
#include "servercontrol.h"

//---------------------------------------------------------------------------

ServerControl* GServer;
ServerControl* Server(){
	return GServer;
}
void SetServer(ServerControl* server){
	GServer = server;
}

ServerControl::ServerControl()
	:PlayerHeader(PLAYERADDY), ObjectTableHeader(OBJECTADDY), alterplayers(ALTERADDY), machinelist(MACHINEADDY), globalserver(GSERVERADDY), debug(false)
{}
ServerControl::ServerControl(Static_Player_Header* pl_head, Object_Table_Header* obj_head, Alter_Player* alt_pl, MachineInfo* machine_list) // , ban_entry* ban_list);
	:PlayerHeader(pl_head), ObjectTableHeader(obj_head), alterplayers(alt_pl), machinelist(machine_list), debug(true)//, gserver(gserver_addy)
{}

void ServerControl::AddCustomCommand(string cmd, CmdFunc function)
{
	map<string, CmdFunc>::iterator match = CustomCommands.find(cmd);
	if(match != CustomCommands.end())
		return; // simply abort adding a duplicate command.
	CustomCommands[cmd] = function;
}
toggle ServerControl::DoCustomCommand(string command)
{
	ArgContainer cmd(command);
	if(cmd.argc==0)
		return CMDNOMATCH;

	if(CustomCommands.find(cmd[0]) == CustomCommands.end())
		return CMDNOMATCH;

	return CustomCommands[cmd[0]](cmd);
}

//---------------------------------------------------------------------------

Static_Player* ServerControl::GetPlayer(playerindex pl_ind)
{
	//return (Static_Player*)&PlayerHeader->Players[pl_ind];
	if(pl_ind > 15 || pl_ind < 0)
		return NULL;
	return (Static_Player*)((unsigned long)PlayerHeader->Players + pl_ind * PlayerHeader->SlotSize);
}

MachineInfo* ServerControl::GetMachine(playerindex pl_ind)
{
	if(!IsOn(pl_ind))
		return NULL;
	return &machinelist[GetPlayer(pl_ind)->MachineIndex];
}
//---------------------------------------------------------------------------

string ServerControl::GetPlayerName(playerindex pl_ind)
{
	return WidetoString(wstring(GetPlayer(pl_ind)->Name));
}
bool ServerControl::IsOn(playerindex pl_ind)
{
	if(pl_ind > 15 || pl_ind < 0)
		return false;
	if(GetPlayer(pl_ind)->PlayerID == -1 || GetPlayer(pl_ind)->PlayerID == 0)
		return false;
	return true;
}
//---------------------------------------------------------------------------

bool ServerControl::IsSpawned(playerindex pl_ind)
{
	if(GetPlayer(pl_ind)->CurrentBiped.index == -1 || GetPlayer(pl_ind)->CurrentBiped.index == 0)
		return false;
	return true;
}
//---------------------------------------------------------------------------

string ServerControl::GetPlayerHash(playerindex pl_ind)
{
	string hash = machinelist[GetPlayer(pl_ind)->MachineIndex].CDhash;
	return hash;
}

//---------------------------------------------------------------------------
string ServerControl::GetMapName()
{
	return globalserver->MAP_NAME;
}

//---------------------------------------------------------------------------

string ServerControl::GetGametypeName()
{
	return WidetoString(wstring(globalserver->GAMETYPE_NAME));
}

//---------------------------------------------------------------------------

string ServerControl::GetServerName()
{
	return WidetoString(wstring(globalserver->SV_NAME));
}

void ServerControl::SetCoord(playerindex pl_ind, D3DXVECTOR3 coord)
{
	ABiped* mc = GetBiped(pl_ind);
	if(mc != NULL)
		mc->World = coord;
}
D3DXVECTOR3 ServerControl::GetCoord(playerindex pl_ind)
{
	ABiped* mc = GetBiped(pl_ind);
	if(mc == NULL)
		return BAD_LOC;
	return mc->World;
}
//---------------------------------------------------------------------------

ABiped* ServerControl::GetBiped(playerindex pl_ind)
{
	if(!IsSpawned(pl_ind))
		return NULL;
	return (ABiped*)ObjectTableHeader->Objects[GetPlayer(pl_ind)->CurrentBiped.index].Offset;
}

AWeapon* ServerControl::GetWeapon(playerindex pl_ind)
{
	//Static_Player* pl = GetPlayer(i);
	ABiped* mc = GetBiped(pl_ind);
	if(mc==NULL)
		return NULL;
	return (AWeapon*)ObjectTableHeader->Objects[mc->Weapon.index].Offset;
}

void ServerControl::SetAmmo(playerindex i, unsigned short ammo)
{
	//Static_Player* pl = GetPlayer(i);
	ABiped* mc = GetBiped(i);
	if(mc==NULL)
		return;
	AWeapon* weap = (AWeapon*)ObjectTableHeader->Objects[mc->Weapon.index].Offset;
	weap->BulletCountInRemainingClips = ammo;
}

void ServerControl::SetTeam(playerindex i, toggle team)
{
	Static_Player* pl = GetPlayer(i);
	if(team == -1)
		team = !pl->Team;
	pl->Team = team;
	pl->iTeam = team;
	alterplayers[i].Team = team;
}

void ServerControl::ExecCmd(const char* command)
{
	void ( __cdecl *ExecuteCommand)(const char *cmd) = ( void(__cdecl*)(const char*) ) 0x0047EA20;
	ExecuteCommand(command);
}
void ServerControl::Print(int something, const char* format)
{
	void (*iPrint)(int r,const char *text,...) = (void(*)(int,const char*,...))0x004b3800;
	iPrint(something, format);

}